package com.bolt.core.view.components;


import com.bolt.core.view.templates.FreemarkerTemplateEngine;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class CombinationFormItem extends FormItem {


    private List <FormItem> combinations;

    public List <FormItem> getCombinations() {
        return combinations;
    }


    @Override
    public void end(HttpServletRequest request, HttpServletResponse response, Writer writer) {
        super.evaluateParams(request);

        Decorate decorate = Decorate.buildCombinationForm(null);
        decorate.start(request, response, writer);
        for (FormItem item : combinations) {
            item.start(request, response, writer);
            item.end(request, response, writer);
        }
        decorate.end(request, response, writer);
    }

    public CombinationFormItem() {
        super();
        combinations = new ArrayList <>();
    }

    public void addCombinationFormItem(FormItem itme) {
        itme.setCombination(true);
        combinations.add(itme);
    }

    @Override
    public void setTemplate(String template) {
        for (FormItem item : combinations) {
            item.setTemplate(template);
        }
    }

    @Override
    public void setTheme(String theme) {
        for (FormItem item : combinations) {
            item.setTheme(theme);
        }
    }

    @Override
    public void setTemplateEngine(FreemarkerTemplateEngine en) {
        for (FormItem item : combinations) {
            item.setTemplateEngine(en);
        }
    }


}
